package com.dnjn.happiness.biz.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnjn.happiness.biz.domain.Outlet;
import com.dnjn.happiness.biz.domain.OutletReply;
import com.dnjn.happiness.biz.domain.vo.OutletExportBean;
import com.dnjn.happiness.biz.domain.vo.OutletVO;
import com.dnjn.happiness.biz.domain.vo.OutletWeekPublishInfoVO;
import com.dnjn.happiness.biz.domain.vo.OutletWeekRankingVO;
import com.dnjn.happiness.biz.domain.vo.OutletWeekValueVO;
import com.dnjn.happiness.biz.enums.OutletStatus;
import com.dnjn.happiness.biz.mapper.OutletMapper;
import com.dnjn.happiness.biz.service.IOutletReplyService;
import com.dnjn.happiness.biz.service.IOutletService;
import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.utils.DateUtils;
import com.dnjn.happiness.common.core.utils.StringUtils;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.system.api.RemoteTeamService;
import com.dnjn.happiness.system.api.domain.SysTeam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * 解忧馆Service业务层处理
 *
 * @author happiness
 * @date 2024-03-13
 */
@Service
public class OutletServiceImpl extends ServiceImpl<OutletMapper, Outlet> implements IOutletService {
    @Autowired
    private IOutletReplyService replyService;

    @Autowired
    private RemoteTeamService teamService;

    @Override
    public Page<OutletVO> getPage(Page page, Outlet outlet) {
        if (Objects.isNull(outlet.getDeptId())) {
            outlet.setDeptId(SecurityUtils.getLoginUser().getDeptId());
        }
        return baseMapper.getPage(page, outlet);
    }

    @Override
    public void urge(Long outletId) {
        Outlet outlet = baseMapper.selectById(outletId);
        outlet.setOutletCount(outlet.getOutletCount() + 1);
        outlet.setOutletTime(LocalDateTime.now());
        int i = baseMapper.updateById(outlet);
    }

    @Override
    public void setOutletField(Outlet outlet) {
        outlet.setOutletTime(LocalDateTime.now());
        outlet.setCreateBy(SecurityUtils.getUsername());
        outlet.setWeekOfYear(DateUtils.getWeekOfYear());
        outlet.setWeek(DateUtils.getWeek());
        outlet.setYear(DateUtils.getYear());
        outlet.setUserId(SecurityUtils.getUserId());
    }


    @Override
    public void getOutletReplyList(List<OutletVO> list) {
        if (!CollectionUtils.isEmpty(list)) {
            for (OutletVO outletVO : list) {
                //判断是否可以催
                LocalDateTime outletTime = outletVO.getOutletTime();
                LocalDateTime now = LocalDateTime.now();
                long between = ChronoUnit.HOURS.between(outletTime, now);
                if (between >= 24 && outletVO.getOutletCount() < 3 && outletVO.getIsReply().equals("N")
                        || outletVO.getOutletCount().equals(0) && outletVO.getIsReply().equals("N")) {
                    outletVO.setIsUrge(true);
                } else {
                    outletVO.setIsUrge(false);
                }
                outletVO.setOutletReplyList(getOutletReplyByOutletId(outletVO.getOutletId()));
                //封装所属团队
                if (Objects.isNull(outletVO.getParentTeamId())) {
                    continue;
                }
                SysTeam parentTeam = null;
                R<SysTeam> result = teamService.queryInfoByTeamId(outletVO.getParentTeamId(), SecurityConstants.INNER);
                if(StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())){
                    parentTeam = result.getData();
                }else {
                    continue;
                }
                outletVO.setParentTeamName(parentTeam.getNickName());
            }
        }
    }

    private List<OutletReply> getOutletReplyByOutletId(Long outletId) {
        return replyService.lambdaQuery().eq(OutletReply::getOutletId, outletId).orderByDesc(OutletReply::getCreateTime).list();
    }


    @Override
    public List<OutletWeekPublishInfoVO> getWeekPublishInfo() {
        List<OutletWeekPublishInfoVO> list = baseMapper.getWeekPublishInfo();
        if (CollectionUtils.isEmpty(list)) {
            return list;
        }
        Integer week = list.get(0).getWeek();
        if (week > 1) {
            for (int j = week - 1; j >= 1; j--) {
                list.add(0, new OutletWeekPublishInfoVO().setWeek(j).setCount(0));
            }
        }
        return list;
    }

    @Override
    public List<OutletWeekValueVO> getWeekPublishInfoValue() {
        List<String> weekList = baseMapper.getWeekList();
        List<OutletWeekValueVO> returnList = new ArrayList<>();
        returnList.add(new OutletWeekValueVO().setStatus(OutletStatus.STATUS_A.getStatus()).setStatusValue(OutletStatus.STATUS_A.getValue()));
        returnList.add(new OutletWeekValueVO().setStatus(OutletStatus.STATUS_B.getStatus()).setStatusValue(OutletStatus.STATUS_B.getValue()));
        returnList.add(new OutletWeekValueVO().setStatus(OutletStatus.STATUS_C.getStatus()).setStatusValue(OutletStatus.STATUS_C.getValue()));
        returnList.add(new OutletWeekValueVO().setStatus(OutletStatus.STATUS_D.getStatus()).setStatusValue(OutletStatus.STATUS_D.getValue()));
        returnList.add(new OutletWeekValueVO().setStatus(OutletStatus.STATUS_S.getStatus()).setStatusValue(OutletStatus.STATUS_S.getValue()));
        for (OutletWeekValueVO vo : returnList) {
            List<OutletWeekValueVO.WeekStatusValue> dataList = new ArrayList<>();
            for (String week : weekList) {
                Integer value = baseMapper.getValueByStatusAndWeek(week, vo.getStatus()) * vo.getStatusValue();
                OutletWeekValueVO.WeekStatusValue weekStatusValue = new OutletWeekValueVO.WeekStatusValue();
                weekStatusValue.setWeek(week);
                weekStatusValue.setValue(value);
                dataList.add(weekStatusValue);
            }
            vo.setData(dataList);
        }
        return returnList;
    }

    @Override
    public List<OutletWeekRankingVO> getWeekRankingAgoTwenty() {

        return baseMapper.getWeekRankingAgoTwenty();
    }

    @Override
    public List<OutletWeekRankingVO> getWeekOutletValueAgoTwenty() {

        return baseMapper.getWeekOutletValueAgoTwenty();
    }

    @Override
    public List<OutletExportBean> getExportList(Outlet outlet) {
        return baseMapper.getExportList(outlet);
    }

    @Override
    public void getExportOutletReplyData(List<OutletExportBean> list) {
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(item -> {
                //查询所属团队
                Long userId = item.getUserId();
                R<SysTeam> result = teamService.getParentInfoByUserId(userId, SecurityConstants.INNER);
                if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
                    item.setTeamName(result.getData().getNickName());
                }
                //封装回复记录信息
                item.setReplyList(replyService.selectReplyListByOutletId(item.getOutletId()));
            });
        }
    }

    @Override
    public Outlet getLastOutletByUserId(Long userId) {
        return baseMapper.getLastOutletByUserId(userId);
    }
}
